<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input
          v-model="queryParams.keyword"
          style="margin-bottom:10px"
          type="text"
          prefix-icon="el-icon-search"
          size="small"
          placeholder="输入员工姓名全员搜索"
          @input="changeList"
        />
        <!-- 树形组件 -->
        <el-tree
          ref="depts"
          node-key="id"
          :data="depts"
          :props="defaultProps"
          :expand-on-click-node="false"
          default-expand-all
          highlight-current
          @current-change="selectNata"
        />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary" @click="$router.push('/employee/detail')">添加员工</el-button>
          <el-button size="mini" @click="showExcelDialog = true">excel导入</el-button>
          <el-button size="mini" @click="exportExcelClick">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <el-table :data="employList">
          <el-table-column prop="staffPhoto" label="头像" width="80px" align="center">
            <template #default="{row}">
              <el-avatar v-if="row.staffPhoto" :size="30" :src="row.staffPhoto" />
              <span v-else class="username">{{ row.username.charAt(0) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="mobile" label="手机号" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column prop="formOfEmployment" label="聘用形式">
            <template #default="{row}">
              <span v-if="row.formOfEmployment === 1">正式</span>
              <span v-else-if="row.formOfEmployment === 2">非正式</span>
              <span v-else>其他</span>
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" label="入职时间" sortable />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button type="text" size="mini" @click="$router.push(`/employee/detail/${row.id}`)">查看</el-button>
              <el-button type="text" size="mini">角色</el-button>
              <el-popconfirm
                title="您确定删除吗？"
                @onConfirm="delEdit(row.id)"
              >
                <el-button slot="reference" type="text" size="mini">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row type="flex" justify="end" style="height:60px">
          <el-pagination
            :current-page="queryParams.page"
            :page-size="queryParams.pagesize"
            layout="total, prev, pager, next"
            :total="queryParams.total"
            @current-change="changePage"
          /></el-row>
      </div>
    </div>
    <import-excel
      :show-excel-dialog.sync="showExcelDialog"
      @unloadSuccess="getEmployee"
    />
  </div>
</template>

<script>
import FileSaver from 'file-saver'
import { transListToTreeData } from '@/utils/index'
import { getDeptsApi } from '@/api/department'
import { getEmployeeAPI, exportExcelAPI, delloadAPI } from '@/api/employee'
import importExcel from './components/import-excel'
export default {
  name: 'Employee',
  components: {
    importExcel
  },
  data() {
    return {
      showExcelDialog: false,
      depts: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      queryParams: {
        departmentId: null,
        total: 0,
        page: 1,
        pagesize: 10,
        keyword: ''
      },
      employList: []
    }
  },
  created() {
    // 获取部门信息
    this.getDeptsList()
    this.getEmployee()
  },
  methods: {
    // 删除员工
    async delEdit(id) {
      // 调取删除接口
      await delloadAPI(id)
      // 提示用户删除成功
      this.$message.success('删除成功')
      // 如果删除的是最后一夜的最后一个员工，判断进入到前一页
      if (this.queryParams.page > 1 && this.employList.length === 1) {
        this.queryParams.page--
      }
      // 重新获取员工列表数据进行渲染
      this.getEmployee()
    },
    // 下载excel
    async exportExcelClick() {
      // 调导出员工excel文件接口方法
      const result = await exportExcelAPI()
      FileSaver.saveAs(result, '员工列表.xlsx')
    },
    // 搜索员工
    changeList() {
      clearTimeout(this.time)
      this.time = setTimeout(() => {
        this.queryParams.page = 1
        this.getEmployee()
      }, 300)
    },
    // 调取部门信息列表接口
    async getEmployee() {
      const { rows, total } = await getEmployeeAPI(this.queryParams)
      this.employList = rows
      this.queryParams.total = total
    },
    selectNata(nodeData) {
      this.queryParams.departmentId = nodeData.id
      // 点那个节点就获取哪个部门信息
      this.getEmployee()
    },
    // 调取部门信息的api
    async getDeptsList() {
      const deptList = await getDeptsApi()
      // 调用递归树形数据方法，转换获取到的数据
      this.depts = transListToTreeData(deptList)
      this.queryParams.departmentId = this.depts[0].id
      this.$nextTick(() => {
        this.$refs.depts.setCurrentKey(this.depts[0].id)
      })
      // 获取员工部门数据
      this.getEmployee()
    },
    // 分页器
    changePage(currentPage) {
      this.queryParams.page = currentPage
      this.getEmployee()
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
  }
}

</style>
